#include "page_mode1.h"
#include "R.h"
#include "wind_mgr.h"
#include "cdlog.h"
#include "tools.h"
#include "project.h"

PageMode1::PageMode1(ViewGroup *wind_page_box)
    : PageBase(wind_page_box, "@layout/page_home", PAGE_MODE1)
{

    // ViewGroup *meiRiSuiXin = (ViewGroup *)dPageLayout->findViewById(demo::R::id::mei_ri_sui_xin);

    // MyPageAdapter *myPageAdapter = new MyPageAdapter(meiRiSuiXin);

    // View *pageMode1 = LayoutInflater::from(getContext())->inflate("@layout/page_home", this);
    // new PageBase((ViewGroup *)pageMode1, "@layour/page_home", PAGE_MODE1);

    ViewGroup *zhengKaoLayout = (ViewGroup *)dPageLayout->findViewById(demo::R::id::layout_zheng_kao);
    zhengKaoLayout->setOnClickListener([this](View &)
                                       { d_windMgr->showPage(PAGE_MODE2); });
    NumberPicker *catalog1Picker = (NumberPicker *)dPageLayout->findViewById(demo::R::id::catalog1);
    ViewGroup *btnXianZheng = (ViewGroup *)dPageLayout->findViewById(demo::R::id::btn_xian_zheng);
    ViewGroup *btnNenKao = (ViewGroup *)dPageLayout->findViewById(demo::R::id::btn_nen_kao);
    ViewGroup *btnXiangZha = (ViewGroup *)dPageLayout->findViewById(demo::R::id::btn_xiang_zha);
    ViewGroup *btnDunJu = (ViewGroup *)dPageLayout->findViewById(demo::R::id::btn_dun_ju);
    btnXianZheng->setOnClickListener([this, catalog1Picker](View &)
                                     { d_windMgr->showPage(PAGE_MODE2, true, 0); });
    btnNenKao->setOnClickListener([this, catalog1Picker](View &)
                                  { d_windMgr->showPage(PAGE_MODE2, true, 1); });
    btnXiangZha->setOnClickListener([this, catalog1Picker](View &)
                                    { d_windMgr->showPage(PAGE_MODE2, true, 2); });
    btnDunJu->setOnClickListener([this, catalog1Picker](View &)
                                 { d_windMgr->showPage(PAGE_MODE2, true, 3); });

    // delete myPageAdapter;

    /*************************************************** */
    // LinearLayout*layout=new LinearLayout(1280,600);
    // layout->setOrientation(LinearLayout::VERTICAL);

    // TabLayout* tab=new TabLayout(1280,36);
    // ViewPager* pager=new ViewPager(1280,560);
    // pager->setOffscreenPageLimit(myPageAdapter->getCount());
    // pager->setOverScrollMode(View::OVER_SCROLL_ALWAYS);
    // pager->setAdapter(myPageAdapter);
    // pager->setBackgroundColor(0xFFF3333);
    // tab->setSelectedTabIndicatorColor(0x8000FF00);
    // tab->setSelectedTabIndicatorHeight(4);
    // tab->setTabIndicatorGravity(Gravity::BOTTOM);//TOP/BOTTOM/CENTER_VERTICAL/FILL_VERTICAL
    // tab->setupWithViewPager(pager);
    // // layout->addView(tab).setId(1);
    // // layout->addView(pager).setId(10);
    // wind_page_box->addView(tab);
    // wind_page_box->addView(pager);MyPageAdapterUp
    // tab->setTabTextColors(0xFFFF0000,0xFF00FF00);

    lunBoAdapterUp = new MyPageAdapterUp();
    ViewPager *viewPagerLayoutUp = (ViewPager *)wind_page_box->findViewById(demo::R::id::view_pager_layout_up);
    viewPagerLayoutUp->setOffscreenPageLimit(lunBoAdapterUp->getCount());
    viewPagerLayoutUp->setOverScrollMode(View::OVER_SCROLL_ALWAYS);
    viewPagerLayoutUp->setAdapter(lunBoAdapterUp);

    TabLayout *tabLayoutUp = (TabLayout *)wind_page_box->findViewById(demo::R::id::tab_layout_up);
    // tabLayout->setSelectedTabIndicatorColor(0xFFFFFF);
    // tabLayout->setSelectedTabIndicatorHeight(2);
    // tabLayout->setTabIndicatorGravity(Gravity::BOTTOM);
    tabLayoutUp->setupWithViewPager(viewPagerLayoutUp);
    // tabLayout->setTabTextColors(0xFFFF0000, 0xFF00FF00);
    // for(int i=0; i<tabLayout->getTabCount();i++){
    //     TabLayout::Tab *t = tabLayout->getTabAt(i);
    //     if(t){
    //         // ViewGroup *ly = (ViewGroup *)LayoutInflater::from(tabLayout->getContext())->inflate("@layout/lunbo_selected_layout", nullptr);
    //         t->setCustomView("@layout/lunbo_selected_layout");
    //     }
    // }
    // tabLayout->setLayoutParams();
    // tabLayout->setpadd
    // tabLayout->setSize(100,100);
    // for (auto aa : lunBoAdapter->getDataList())
    // {
    //     // tabLayout->getTabAt(aa.pos)->setText(aa.name);
    //     LOGE("----------POS%d:NAME%s------------------", aa.pos, App::getInstance().getString(aa.name).c_str());
    // }

    lunBoAdapterDown = new MyPageAdapterDown();

    ViewPager *viewPagerLayoutDown = (ViewPager *)wind_page_box->findViewById(demo::R::id::view_pager_layout_down);
    viewPagerLayoutDown->setOffscreenPageLimit(lunBoAdapterDown->getCount());
    viewPagerLayoutDown->setOverScrollMode(View::OVER_SCROLL_ALWAYS);
    viewPagerLayoutDown->setAdapter(lunBoAdapterDown);

    TabLayout *tabLayoutDown = (TabLayout *)wind_page_box->findViewById(demo::R::id::tab_layout_down);
    tabLayoutDown->setupWithViewPager(viewPagerLayoutDown);

    /**
     *
     * 出水模式管理入口
     * recyclerView 测试
     */

    // TextView *waterTextView = (TextView *)wind_page_box->findViewById(demo::R::id::page_water_mode);
    // waterTextView->setOnClickListener([this](View &){
    //     d_windMgr->showPage(PAGE_WATER_MODE);
    // });
    mTools->setOnClickListenerToShowPage((ViewGroup *)wind_page_box->findViewById(demo::R::id::page_water_mode), PAGE_WATER_MODE);

    // if (viewPagerLayoutUp->getChildCount() >= 1)
    // {
    //     viewPagerUp2 = (ViewGroup *)viewPagerLayoutUp->getChildAt(1);
    //     temperatureNum = (TextView *)viewPagerUp2->findViewById(demo::R::id::temperature_num);
    //     date = (TextView *)viewPagerUp2->findViewById(demo::R::id::date);
    //     weather = (ImageView *)viewPagerUp2->findViewById(demo::R::id::weather);
    // }
    // else
    // {
    //     LOGE("ViewPager ERROR!");
    // }

    // myTickerForWeather = std::bind(&PageMode1::onTickerForWeather, this);
    // myTickerForDay = std::bind(&PageMode1::onTickerForDay, this);

    // onTickerForWeather();onTickerForDay();
    // dPageLayout->postDelayed(myTickerForDay, myTickerTimeForDay);
    // lunBoAdapterUp->notifyDataSetChanged();
    TextView *t1 = (TextView *) wind_page_box->findViewById(demo::R::id::t1);
    t1->setOnClickListener([this](View &)
                           { lunBoAdapterUp->notifyDataSetChanged(); });
}

// void PageMode1::onTickerForWeather()
// {
//     weather->setImageResource(g_appData.weather);
//     temperatureNum->setText(std::to_string(g_appData.temperature));

//     viewPagerUp2->postDelayed(myTickerForWeather, myTickerTimeForWeather);
// }

void PageMode1::onTickerForDay()
{
    // date->setText(getMonthDayDateTime() + " " + getWeekDay());
    lunBoAdapterUp->notifyDataSetChanged();
    // viewPagerUp2->postDelayed(myTickerForDay, myTickerTimeForDay);
}

PageMode1::~PageMode1()
{
    // viewPagerUp2->removeCallbacks(myTickerForWeather);
    // viewPagerUp2->removeCallbacks(myTickerForDay);
    delete lunBoAdapterDown;
    delete lunBoAdapterUp;
}

void PageMode1::onClickListener(View &v)
{
    switch (v.getId())
    {
    case demo::R::id::page_water_mode:
    {
        break;
    }
    }
}

void PageMode1::onMsg(int type, void *data)
{
    switch (type)
    {
    case MSG_UPDATE_DATA:
    {
        LOGE("MSG_UPDATE_DATA");
        LOGE("weather = %s", g_appData.weather.c_str());
        LOGE("temperature = %d", g_appData.temperature);
        // lunBoAdapterUp->updateItem(1);
        lunBoAdapterUp->notifyDataSetChanged();
    }   break;
    default:
        break;
    }
}